package com.atguigu.jdbc2;

import com.atguigu.jdbc.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BaseDAO2 {
    private QueryRunner queryRunner = new QueryRunner();

    public int update(Connection connection, String sql, Object... obj) {
        sql = "insert into demo1(id,username) values(?,?)";
        try {
            return queryRunner.update(connection, sql, obj);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    public void test1() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select * from demo1 where id = ?";
        Employee query = queryRunner.query(JDBCUtils.getConnection(), sql, new BeanHandler<Employee>(Employee.class), 15);
        System.out.println(query);

    }
    public List<Employee>  query(Connection connection,String sql,Class clazz,Object... obj) throws SQLException {
        return queryRunner.query(connection, sql, new BeanListHandler<Employee>(clazz), obj);


    }

}
